<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Credit
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: Core.php 2012-01-04 13:05 teajay $
 * @author     Taalay
 */

/**
 * @category   Application_Extensions
 * @package    Credit
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Credit_Plugin_Core
{
  public function onItemCreateAfter($event)
  {
    /**
     * @var $api Credit_Api_Core
     */
    $object = $event->getPayload();
    $api = Engine_Api::_()->credit();
    if ($object->getType() == 'activity_action') {
      $user = Engine_Api::_()->getItem('user', $object->subject_id);
      $api->updateCredits($user, $object);
    } else {
      $api->updateItemCredits(Engine_Api::_()->user()->getViewer(), $object);
    }
  }

  public function onUserLoginAfter($event)
  {
    /**
     * @var $api Credit_Api_Core
     */
    $user = $event->getPayload();
    $api = Engine_Api::_()->credit();
    $api->updateItemCredits($user, $user);
  }

  public function onInviterSendInvite($event)
  {
    /**
     * @var $api Credit_Api_Core
     */
    $invite = $event->getPayload();
    $api = Engine_Api::_()->credit();
    $api->updateInviteCredits($invite);
  }

  public function onInviterRefered($event)
  {
    /**
     * @var $api Credit_Api_Core
     */
    $invite = $event->getPayload();
    $api = Engine_Api::_()->credit();
    $api->updateInviteCredits($invite);
  }

  public function onUserDeleteBefore($event)
  {
    /**
     * @var $user User_Model_User
     * @var $balance Credit_Model_Balance
     */

    $user = $event->getPayload();
    $balance = Engine_Api::_()->getItem('credit_balance', $user->getIdentity());
    if ($balance) {
      $balance->delete();
    }
  }
}